/*
 * AisPView.java
 */

package aisp;

import org.jdesktop.application.Action;
import org.jdesktop.application.ResourceMap;
import org.jdesktop.application.SingleFrameApplication;
import org.jdesktop.application.FrameView;
import org.jdesktop.application.TaskMonitor;

import wrappers.*;

import dataProviders.*;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;

import javax.swing.JButton;
import javax.swing.Timer;
import javax.swing.Icon;
import javax.swing.JDialog;
import javax.swing.JFrame;

/**
 * The application's main frame.
 */
public class AisPView extends FrameView {
	int linkAction = 1;
    public AisPView(SingleFrameApplication app) {
        super(app);
        dp = new DataProvider();
        initComponents();
        
        
        ecb.setEntities(dp.entity);
        ecb1.setEntities(dp.entity);
        ecb2.setEntities(dp.entity);
        dtcb.setDataTypes(dp.dataType);
        dtcb2.setDataTypes(dp.dataType);
        lcb.setData(dp.entity, dp.link);

        
        
        // status bar initialization - message timeout, idle icon and busy animation, etc
        ResourceMap resourceMap = getResourceMap();
        int messageTimeout = resourceMap.getInteger("StatusBar.messageTimeout");
        messageTimer = new Timer(messageTimeout, new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                statusMessageLabel.setText("");
            }
        });
        messageTimer.setRepeats(false);
        int busyAnimationRate = resourceMap.getInteger("StatusBar.busyAnimationRate");
        for (int i = 0; i < busyIcons.length; i++) {
            busyIcons[i] = resourceMap.getIcon("StatusBar.busyIcons[" + i + "]");
        }
        busyIconTimer = new Timer(busyAnimationRate, new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                busyIconIndex = (busyIconIndex + 1) % busyIcons.length;
                statusAnimationLabel.setIcon(busyIcons[busyIconIndex]);
            }
        });
        idleIcon = resourceMap.getIcon("StatusBar.idleIcon");
        statusAnimationLabel.setIcon(idleIcon);
        progressBar.setVisible(false);

        // connecting action tasks to status bar via TaskMonitor
        TaskMonitor taskMonitor = new TaskMonitor(getApplication().getContext());
        taskMonitor.addPropertyChangeListener(new java.beans.PropertyChangeListener() {
            public void propertyChange(java.beans.PropertyChangeEvent evt) {
                String propertyName = evt.getPropertyName();
                if ("started".equals(propertyName)) {
                    if (!busyIconTimer.isRunning()) {
                        statusAnimationLabel.setIcon(busyIcons[0]);
                        busyIconIndex = 0;
                        busyIconTimer.start();
                    }
                    progressBar.setVisible(true);
                    progressBar.setIndeterminate(true);
                } else if ("done".equals(propertyName)) {
                    busyIconTimer.stop();
                    statusAnimationLabel.setIcon(idleIcon);
                    progressBar.setVisible(false);
                    progressBar.setValue(0);
                } else if ("message".equals(propertyName)) {
                    String text = (String)(evt.getNewValue());
                    statusMessageLabel.setText((text == null) ? "" : text);
                    messageTimer.restart();
                } else if ("progress".equals(propertyName)) {
                    int value = (Integer)(evt.getNewValue());
                    progressBar.setVisible(true);
                    progressBar.setIndeterminate(false);
                    progressBar.setValue(value);
                }
            }
        });
    }

    @Action
    public void showAboutBox() {
        if (aboutBox == null) {
            JFrame mainFrame = AisPApp.getApplication().getMainFrame();
            aboutBox = new AisPAboutBox(mainFrame);
            aboutBox.setLocationRelativeTo(mainFrame);
        }
        AisPApp.getApplication().show(aboutBox);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        mainPanel = new javax.swing.JPanel();
        jTabbedPane1 = new javax.swing.JTabbedPane();
        entityPanel = new javax.swing.JPanel();
        topEntity = new javax.swing.JPanel();
        entitySelection = new javax.swing.JPanel();
        entityChooseComboBox = new javax.swing.JComboBox();
        addEntityPanel = new javax.swing.JPanel();
        addEntityTextField = new javax.swing.JTextField();
        addEntityButton = new javax.swing.JButton();
        bottomEntity = new javax.swing.JPanel();
        editPropertyPanel = new javax.swing.JPanel();
        editPropertyButtonsPanel = new javax.swing.JPanel();
        savePropertyButton = new javax.swing.JButton();
        deletePropertyButton = new javax.swing.JButton();
        editPropertyPropertiesPanel = new javax.swing.JPanel();
        editPropertyComboBox = new javax.swing.JComboBox();
        editPropertyTextField = new javax.swing.JTextField();
        addPropertyPanel = new javax.swing.JPanel();
        addPropertyButton = new javax.swing.JButton();
        addProperyPropertiesPanel = new javax.swing.JPanel();
        propertyAddComboBox = new javax.swing.JComboBox();
        addPropertyTextField = new javax.swing.JTextField();
        centerEntity = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        entityTable = new javax.swing.JTable();
        linkPanel = new javax.swing.JPanel();
        topLinkPanel = new javax.swing.JPanel();
        jComboBox1 = new javax.swing.JComboBox();
        topLinkButtonPanel = new javax.swing.JPanel();
        editLinkButton = new javax.swing.JButton();
        deleteLinkButton = new javax.swing.JButton();
        newLinkButton = new javax.swing.JButton();
        linkEditPanel = new javax.swing.JPanel();
        firstEntityLinkComboBox = new javax.swing.JComboBox();
        linkTypeComboBox = new javax.swing.JComboBox();
        secondEntityLinkComboBox = new javax.swing.JComboBox();
        commitButton = new javax.swing.JButton();
        generatePanel = new javax.swing.JPanel();
        generateSQLButton = new javax.swing.JButton();
        jScrollPane2 = new javax.swing.JScrollPane();
        genereateSQLTextArea = new javax.swing.JTextArea();
        statusPanel = new javax.swing.JPanel();
        statusPanelSeparator = new javax.swing.JSeparator();
        statusMessageLabel = new javax.swing.JLabel();
        statusAnimationLabel = new javax.swing.JLabel();
        progressBar = new javax.swing.JProgressBar();

        mainPanel.setName("mainPanel"); // NOI18N
        mainPanel.setLayout(new javax.swing.BoxLayout(mainPanel, javax.swing.BoxLayout.LINE_AXIS));

        jTabbedPane1.setName("jTabbedPane1"); // NOI18N

        entityPanel.setName("entityPanel"); // NOI18N
        entityPanel.setLayout(new java.awt.BorderLayout());

        topEntity.setName("topEntity"); // NOI18N
        topEntity.setLayout(new java.awt.BorderLayout());

        entitySelection.setName("entitySelection"); // NOI18N
        entitySelection.setLayout(new java.awt.BorderLayout());

        entityChooseComboBox.setModel(ecb);
        entityChooseComboBox.setName("entityChooseComboBox"); // NOI18N
        entityChooseComboBox.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                entityChooseComboBoxActionPerformed(evt);
            }
        });
        entitySelection.add(entityChooseComboBox, java.awt.BorderLayout.CENTER);
        JButton deleteEntityButton = new JButton();
        deleteEntityButton.setText("Delete entity");
        deleteEntityButton.setName("deleteEntityButton"); // NOI18N
        deleteEntityButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
            	dp.deleteEntity(chosenEntity);
            	reloadECBs();
            	reloadLCB();
            	reloadPTM();
            }
        });
		
        entitySelection.add(deleteEntityButton, java.awt.BorderLayout.LINE_END);
        
        
        topEntity.add(entitySelection, java.awt.BorderLayout.PAGE_START);

        addEntityPanel.setName("addEntityPanel"); // NOI18N
        addEntityPanel.setLayout(new java.awt.BorderLayout());

        org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(aisp.AisPApp.class).getContext().getResourceMap(AisPView.class);
        addEntityTextField.setText(resourceMap.getString("addEntityTextField.text")); // NOI18N
        addEntityTextField.setName("addEntityTextField"); // NOI18N
        addEntityPanel.add(addEntityTextField, java.awt.BorderLayout.CENTER);

        addEntityButton.setText(resourceMap.getString("addEntityButton.text")); // NOI18N
        addEntityButton.setName("addEntityButton"); // NOI18N
        addEntityButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                addEntityButtonActionPerformed(evt);
            }
        });
		
        addEntityPanel.add(addEntityButton, java.awt.BorderLayout.LINE_END);

		
		
        topEntity.add(addEntityPanel, java.awt.BorderLayout.CENTER);

        entityPanel.add(topEntity, java.awt.BorderLayout.PAGE_START);

        bottomEntity.setName("bottomEntity"); // NOI18N
        bottomEntity.setLayout(new java.awt.BorderLayout());

        editPropertyPanel.setName("editPropertyPanel"); // NOI18N
        editPropertyPanel.setLayout(new java.awt.BorderLayout());

        editPropertyButtonsPanel.setName("editPropertyButtonsPanel"); // NOI18N
        editPropertyButtonsPanel.setLayout(new javax.swing.BoxLayout(editPropertyButtonsPanel, javax.swing.BoxLayout.LINE_AXIS));

        savePropertyButton.setText(resourceMap.getString("savePropertyButton.text")); // NOI18N
        savePropertyButton.setName("savePropertyButton"); // NOI18N
        savePropertyButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                savePropertyButtonActionPerformed(evt);
            }
        });
        editPropertyButtonsPanel.add(savePropertyButton);

        deletePropertyButton.setText(resourceMap.getString("deletePropertyButton.text")); // NOI18N
        deletePropertyButton.setName("deletePropertyButton"); // NOI18N
        deletePropertyButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                deletePropertyButtonActionPerformed(evt);
            }
        });
        editPropertyButtonsPanel.add(deletePropertyButton);

        editPropertyPanel.add(editPropertyButtonsPanel, java.awt.BorderLayout.LINE_END);

        editPropertyPropertiesPanel.setName("editPropertyPropertiesPanel"); // NOI18N
        editPropertyPropertiesPanel.setLayout(new java.awt.BorderLayout());

        editPropertyComboBox.setModel(dtcb);
        editPropertyComboBox.setName("editPropertyComboBox"); // NOI18N
        editPropertyPropertiesPanel.add(editPropertyComboBox, java.awt.BorderLayout.LINE_END);

        editPropertyTextField.setText(resourceMap.getString("editPropertyTextField.text")); // NOI18N
        editPropertyTextField.setName("editPropertyTextField"); // NOI18N
        editPropertyPropertiesPanel.add(editPropertyTextField, java.awt.BorderLayout.CENTER);
        editPropertyTextField.setEnabled(false);
        editPropertyPanel.add(editPropertyPropertiesPanel, java.awt.BorderLayout.CENTER);

        bottomEntity.add(editPropertyPanel, java.awt.BorderLayout.PAGE_START);

        addPropertyPanel.setName("addPropertyPanel"); // NOI18N
        addPropertyPanel.setLayout(new java.awt.BorderLayout());

        addPropertyButton.setText(resourceMap.getString("addPropertyButton.text")); // NOI18N
        addPropertyButton.setName("addPropertyButton"); // NOI18N
        addPropertyButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                addPropertyButtonActionPerformed(evt);
            }
        });
        addPropertyPanel.add(addPropertyButton, java.awt.BorderLayout.LINE_END);

        addProperyPropertiesPanel.setName("addProperyPropertiesPanel"); // NOI18N
        addProperyPropertiesPanel.setLayout(new java.awt.BorderLayout());

        propertyAddComboBox.setModel(dtcb2);
        propertyAddComboBox.setName("propertyAddComboBox"); // NOI18N
        addProperyPropertiesPanel.add(propertyAddComboBox, java.awt.BorderLayout.LINE_END);

        addPropertyTextField.setText(resourceMap.getString("addPropertyTextField.text")); // NOI18N
        addPropertyTextField.setName("addPropertyTextField"); // NOI18N
        addProperyPropertiesPanel.add(addPropertyTextField, java.awt.BorderLayout.CENTER);

        addPropertyPanel.add(addProperyPropertiesPanel, java.awt.BorderLayout.CENTER);

        bottomEntity.add(addPropertyPanel, java.awt.BorderLayout.PAGE_END);

        entityPanel.add(bottomEntity, java.awt.BorderLayout.PAGE_END);

        centerEntity.setName("centerEntity"); // NOI18N
        centerEntity.setLayout(new java.awt.BorderLayout());

        jScrollPane1.setName("jScrollPane1"); // NOI18N

        entityTable.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {

            }
        ));
        entityTable.setName("entityTable"); // NOI18N
        entityTable.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                entityTableMouseClicked(evt);
            }
        });
        jScrollPane1.setViewportView(entityTable);

        centerEntity.add(jScrollPane1, java.awt.BorderLayout.CENTER);

        entityPanel.add(centerEntity, java.awt.BorderLayout.CENTER);

        jTabbedPane1.addTab(resourceMap.getString("entityPanel.TabConstraints.tabTitle"), entityPanel); // NOI18N

        linkPanel.setName("linkPanel"); // NOI18N
        linkPanel.setLayout(new java.awt.BorderLayout());

        topLinkPanel.setName("topLinkPanel"); // NOI18N
        topLinkPanel.setLayout(new java.awt.BorderLayout());

        jComboBox1.setModel(lcb);
        jComboBox1.setName("jComboBox1"); // NOI18N
        
        topLinkPanel.add(jComboBox1, java.awt.BorderLayout.CENTER);

        topLinkButtonPanel.setName("topLinkButtonPanel"); // NOI18N
        topLinkButtonPanel.setLayout(new javax.swing.BoxLayout(topLinkButtonPanel, javax.swing.BoxLayout.LINE_AXIS));

        editLinkButton.setText(resourceMap.getString("editLinkButton.text")); // NOI18N
        editLinkButton.setName("editLinkButton"); // NOI18N
        editLinkButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                linkAction = 0;
                Link temp = lcb.getSelectedLink();
                currentEditedLink = temp.getId();
                Object a = ecb1.findItem(temp.getSource());
                ecb1.setSelectedItem(a);
                firstEntityLinkComboBox.setEnabled(false);
                a = ecb2.findItem(temp.getTarget());
                ecb2.setSelectedItem(a);
                secondEntityLinkComboBox.setEnabled(false);
                linkTypeComboBox.setSelectedIndex(temp.getSourceType()+temp.getTargetType()*2);
            }
        });
        topLinkButtonPanel.add(editLinkButton);

        deleteLinkButton.setText(resourceMap.getString("deleteLinkButton.text")); // NOI18N
        deleteLinkButton.setName("deleteLinkButton"); // NOI18N
        deleteLinkButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                dp.deleteLink(lcb.getSelectedId());
                reloadLCB();
            }
        });
        topLinkButtonPanel.add(deleteLinkButton);

        newLinkButton.setText(resourceMap.getString("newLinkButton.text")); // NOI18N
        newLinkButton.setName("newLinkButton"); // NOI18N
        newLinkButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                linkAction = 1;
                firstEntityLinkComboBox.setEnabled(true);
                secondEntityLinkComboBox.setEnabled(true);
                reloadECBs();
                
            }
        });
        topLinkButtonPanel.add(newLinkButton);

        topLinkPanel.add(topLinkButtonPanel, java.awt.BorderLayout.LINE_END);

        linkPanel.add(topLinkPanel, java.awt.BorderLayout.PAGE_START);

        linkEditPanel.setName("linkEditPanel"); // NOI18N
        linkEditPanel.setLayout(new javax.swing.BoxLayout(linkEditPanel, javax.swing.BoxLayout.LINE_AXIS));

        firstEntityLinkComboBox.setModel(ecb1);
        firstEntityLinkComboBox.setName("firstEntityLinkComboBox"); // NOI18N
        linkEditPanel.add(firstEntityLinkComboBox);

        linkTypeComboBox.setModel(new javax.swing.DefaultComboBoxModel(dp.multType));
        linkTypeComboBox.setName("linkTypeComboBox"); // NOI18N
        linkTypeComboBox.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                System.out.println(linkTypeComboBox.getSelectedIndex()+" "+linkTypeComboBox.getSelectedItem());
                }
            });
        linkEditPanel.add(linkTypeComboBox);

        secondEntityLinkComboBox.setModel(ecb2);
        secondEntityLinkComboBox.setName("secondEntityLinkComboBox"); // NOI18N
        linkEditPanel.add(secondEntityLinkComboBox);

        commitButton.setText(resourceMap.getString("commitButton.text")); // NOI18N
        commitButton.setName("commitButton"); // NOI18N
        commitButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
            	System.out.println(linkAction);
                switch (linkAction) {
				case 0:
					int mult = linkTypeComboBox.getSelectedIndex();
					System.out.println("Multiplicity of edited link "+mult);
					dp.editMultiplicity(currentEditedLink, mult);
					reloadLCB();
					break;
				case 1:
					int indexSource = ecb1.getSelectedId();
					int indexTarget = ecb2.getSelectedId();
					int mult1 = linkTypeComboBox.getSelectedIndex();
					dp.addLink(indexSource, indexTarget, mult1);
					break;
				default:
					break;
				}
                reloadLCB();
            }
        });
        linkEditPanel.add(commitButton);

        linkPanel.add(linkEditPanel, java.awt.BorderLayout.PAGE_END);

        jTabbedPane1.addTab(resourceMap.getString("linkPanel.TabConstraints.tabTitle"), linkPanel); // NOI18N

        generatePanel.setName("generatePanel"); // NOI18N
        generatePanel.setLayout(new java.awt.BorderLayout());

        generateSQLButton.setText(resourceMap.getString("generateSQLButton.text")); // NOI18N
        generateSQLButton.setName("generateSQLButton"); // NOI18N
        generateSQLButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                generateSQLButtonActionPerformed(evt);
            }
        });
        generatePanel.add(generateSQLButton, java.awt.BorderLayout.PAGE_END);

        jScrollPane2.setName("jScrollPane2"); // NOI18N

        genereateSQLTextArea.setColumns(20);
        genereateSQLTextArea.setRows(5);
        genereateSQLTextArea.setName("genereateSQLTextArea"); // NOI18N
        jScrollPane2.setViewportView(genereateSQLTextArea);

        generatePanel.add(jScrollPane2, java.awt.BorderLayout.CENTER);

        jTabbedPane1.addTab(resourceMap.getString("generatePanel.TabConstraints.tabTitle"), generatePanel); // NOI18N

        mainPanel.add(jTabbedPane1);

        statusPanel.setName("statusPanel"); // NOI18N

        statusPanelSeparator.setName("statusPanelSeparator"); // NOI18N

        statusMessageLabel.setName("statusMessageLabel"); // NOI18N

        statusAnimationLabel.setHorizontalAlignment(javax.swing.SwingConstants.LEFT);
        statusAnimationLabel.setName("statusAnimationLabel"); // NOI18N

        progressBar.setName("progressBar"); // NOI18N

        javax.swing.GroupLayout statusPanelLayout = new javax.swing.GroupLayout(statusPanel);
        statusPanel.setLayout(statusPanelLayout);
        statusPanelLayout.setHorizontalGroup(
            statusPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(statusPanelSeparator, javax.swing.GroupLayout.DEFAULT_SIZE, 400, Short.MAX_VALUE)
            .addGroup(statusPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(statusMessageLabel)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 230, Short.MAX_VALUE)
                .addComponent(progressBar, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(statusAnimationLabel)
                .addContainerGap())
        );
        statusPanelLayout.setVerticalGroup(
            statusPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(statusPanelLayout.createSequentialGroup()
                .addComponent(statusPanelSeparator, javax.swing.GroupLayout.PREFERRED_SIZE, 2, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGroup(statusPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(statusMessageLabel)
                    .addComponent(statusAnimationLabel)
                    .addComponent(progressBar, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(3, 3, 3))
        );

        setComponent(mainPanel);
        setStatusBar(statusPanel);
    }// </editor-fold>//GEN-END:initComponents

    private void entityChooseComboBoxActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_entityChooseComboBoxActionPerformed
        chosenEntity = ecb.getSelectedId(entityChooseComboBox.getSelectedIndex());
        //System.out.println(chosenEntity);
    	reloadPTM();
    }//GEN-LAST:event_entityChooseComboBoxActionPerformed

    
    private void reloadECBs(){
		ecb = new EntityComboBox();
		ecb1 = new EntityComboBox();
		ecb2 = new EntityComboBox();
		ecb.setEntities(dp.entity);
		ecb1.setEntities(dp.entity);
		ecb2.setEntities(dp.entity);
		entityChooseComboBox.setModel(ecb);
		firstEntityLinkComboBox.setModel(ecb1);
		secondEntityLinkComboBox.setModel(ecb2);	
    }
    
    private void reloadLCB(){
		lcb = new LinkComboBox();
		lcb.setData(dp.entity, dp.link);
		jComboBox1.setModel(lcb);
    }
    
    private void reloadPTM(){
		ptm = new PropertyTableModel();
		ArrayList<Property> pts = new ArrayList<Property>();
		ArrayList<Property> temp = dp.property;
		for (Property property : temp) {
			if (property.getEntityId() == chosenEntity){
				pts.add(property);
				//System.out.println(property.getEntityId()+" "+property.getPropertyId()+" "+property.getName()
				//		+" "+dp.dataType.toArray()[property.getType()].toString());
			}
		}
		ptm.setProperties(pts, dp.dataType);
		entityTable.setModel(ptm);
    }
    
    private void addEntityButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_addEntityButtonActionPerformed
        String tempName = addEntityTextField.getText();
        if (!tempName.equals("")) {
			dp.addEntity(tempName);
			reloadECBs();
		} else {
			addEntityTextField.setText("Entity name can't be blank");
		}
    }//GEN-LAST:event_addEntityButtonActionPerformed
    
    private void deletePropertyButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_deletePropertyButtonActionPerformed
        int chosenLine = entityTable.getSelectedRow();
    	for (Property properties : dp.property) {
    		System.out.println(properties.getEntityId()+" "+chosenEntity+" "+properties.getName()+" "+editPropertyTextField.getText());
			if(properties.getEntityId()==chosenEntity){
				if (properties.getName().equals(editPropertyTextField.getText())) {
					dp.deleteProperty(properties.getPropertyId());
					reloadPTM();
					return;
				}
			}
		}
    }//GEN-LAST:event_deletePropertyButtonActionPerformed

    private void savePropertyButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_savePropertyButtonActionPerformed
        int chosenLine = entityTable.getSelectedRow();
    	for (Property properties : dp.property) {
    		System.out.println(properties.getEntityId()+" "+chosenEntity+" "+properties.getName()+" "+editPropertyTextField.getText());
			if(properties.getEntityId()==chosenEntity){
				if (properties.getName().equals(editPropertyTextField.getText())) {
					dp.editProperty(properties.getPropertyId(),editPropertyComboBox.getSelectedIndex());
					reloadPTM();
					return;
				}
			}
		}
    }//GEN-LAST:event_savePropertyButtonActionPerformed

    private void addPropertyButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_addPropertyButtonActionPerformed
        dp.addProperty(chosenEntity, addPropertyTextField.getText(), propertyAddComboBox.getSelectedIndex());
        reloadPTM();
    }//GEN-LAST:event_addPropertyButtonActionPerformed

    private void entityTableMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_entityTableMouseClicked
       chosenLine = entityTable.getSelectedRow();
       editPropertyTextField.setText(ptm.getValueAt(chosenLine, 0).toString());
       String tempType = ptm.getValueAt(chosenLine, 1).toString();
       for (int i = 0; i < dp.dataType.toArray().length; i++) {
    	   if(dp.dataType.toArray()[i].toString().equals(tempType)){
    		   editPropertyComboBox.setSelectedIndex(i);
    		   //System.out.println(i+" "+dp.dataType.toArray()[i].toString());
    	   }
       }    
    }//GEN-LAST:event_entityTableMouseClicked

    private void generateSQLButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_generateSQLButtonActionPerformed
        for (Entity entity : dp.entity) {
			gs.addTable(entity.getName());
			for (Property property : dp.property) {
				if (property.getEntityId()==entity.getId()) {
					gs.addProperty(entity.getName(), property.getName(), dp.dataType.get(property.getType()).getSqlType());
				}
			}
		}
        for (Link link : dp.link) {
			String source ="";
			String target ="";
        	for (int i = 0; i < dp.entity.size(); i++) {
				if(link.getSource()==dp.entity.get(i).getId())
					source = dp.entity.get(i).getName();
				if(link.getTarget()==dp.entity.get(i).getId())
					target = dp.entity.get(i).getName();
			}
        	gs.addLink(source, target, link.getSourceType()+link.getTargetType()*2);
		}
        genereateSQLTextArea.setText(gs.toString());
        gs.clearStatement();
        System.out.println(gs.toString().length());
    }//GEN-LAST:event_generateSQLButtonActionPerformed


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton addEntityButton;
    private javax.swing.JPanel addEntityPanel;
    private javax.swing.JTextField addEntityTextField;
    private javax.swing.JButton addPropertyButton;
    private javax.swing.JPanel addPropertyPanel;
    private javax.swing.JTextField addPropertyTextField;
    private javax.swing.JPanel addProperyPropertiesPanel;
    private javax.swing.JPanel bottomEntity;
    private javax.swing.JPanel centerEntity;
    private javax.swing.JButton commitButton;
    private javax.swing.JButton deleteLinkButton;
    private javax.swing.JButton deletePropertyButton;
    private javax.swing.JButton editLinkButton;
    private javax.swing.JPanel editPropertyButtonsPanel;
    private javax.swing.JComboBox editPropertyComboBox;
    private javax.swing.JPanel editPropertyPanel;
    private javax.swing.JPanel editPropertyPropertiesPanel;
    private javax.swing.JTextField editPropertyTextField;
    private javax.swing.JComboBox entityChooseComboBox;
    private javax.swing.JPanel entityPanel;
    private javax.swing.JPanel entitySelection;
    private javax.swing.JTable entityTable;
    private javax.swing.JComboBox firstEntityLinkComboBox;
    private javax.swing.JPanel generatePanel;
    private javax.swing.JButton generateSQLButton;
    private javax.swing.JTextArea genereateSQLTextArea;
    private javax.swing.JComboBox jComboBox1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JTabbedPane jTabbedPane1;
    private javax.swing.JPanel linkEditPanel;
    private javax.swing.JPanel linkPanel;
    private javax.swing.JComboBox linkTypeComboBox;
    private javax.swing.JPanel mainPanel;
    private javax.swing.JButton newLinkButton;
    private javax.swing.JProgressBar progressBar;
    private javax.swing.JComboBox propertyAddComboBox;
    private javax.swing.JButton savePropertyButton;
    private javax.swing.JComboBox secondEntityLinkComboBox;
    private javax.swing.JLabel statusAnimationLabel;
    private javax.swing.JLabel statusMessageLabel;
    private javax.swing.JPanel statusPanel;
    private javax.swing.JSeparator statusPanelSeparator;
    private javax.swing.JPanel topEntity;
    private javax.swing.JPanel topLinkButtonPanel;
    private javax.swing.JPanel topLinkPanel;
    // End of variables declaration//GEN-END:variables

    private final Timer messageTimer;
    private final Timer busyIconTimer;
    private final Icon idleIcon;
    private final Icon[] busyIcons = new Icon[15];
    private int busyIconIndex = 0;

    private JDialog aboutBox;
    private DataProvider dp;
    private EntityComboBox ecb = new EntityComboBox();
    private EntityComboBox ecb1 = new EntityComboBox();
    private EntityComboBox ecb2 = new EntityComboBox();
    private DataTypeComboBox dtcb = new DataTypeComboBox();
    private DataTypeComboBox dtcb2 = new DataTypeComboBox();
    private LinkComboBox lcb = new LinkComboBox();
    private int currentEditedLink = 0;
    private PropertyTableModel ptm = new PropertyTableModel();
    private int chosenEntity = 1;
    private int chosenLine= 0;
    private GenerateSQL gs = new GenerateSQL(); 
}
